package com.rockxin.cat.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.adapter.BankListAdapter;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.bean.UseBean;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Subscriber;

/**
 * Created by cat on 18-3-9.
 */

public class BankListActivity extends BaseActivity {
    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyler_view)
    RecyclerView recylerView;

    private List<UseBean> bankList;

    private BankListAdapter mAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_bank_list;
    }

    @Override
    public void initData() {
        initView();
        initRecyclerView();
        setListener();
        getData();
    }

    public void initView() {
        title.setText("银行列表");
    }

    private void initRecyclerView() {
        bankList = new ArrayList<>();

        mAdapter = new BankListAdapter(R.layout.item_bank, bankList);
        recylerView.setAdapter(mAdapter);

        final LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recylerView.setLayoutManager(mLayoutManager);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                UseBean data = (UseBean) adapter.getItem(position);
                EventBus.getDefault().post(data, "bank_list");
                finish();
            }
        });
    }

    private void setListener() {
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void getData() {
        final CatNetWork catNetWork = new CatNetWork();
        catNetWork.getbankList(new Subscriber<HttpResult<List<UseBean>>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult<List<UseBean>> listHttpResult) {
                Logger.json(new Gson().toJson(listHttpResult));
                if (listHttpResult.getStatus() == 1) {
                    bankList.addAll(listHttpResult.getData());
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

}
